using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using PriorityQueue;

namespace MUSA
{
    class GroupLeader : Microsoft.Xna.Framework.GameComponent
    {
        #region Member Variables

        //game instance
        protected Game mGame = null;

        //group information
        protected Vector3 mCurrentPoint;
        protected TeamColor mGroupColor;

        //list of tanks in group
        List<Tank> mTankArray = new List<Tank>();

        //note to Hoa: why is this here
        //PriorityQueue<Action> mQueue = new PriorityQueue<Action>();

        #endregion Member Variables


        #region Constructor

        public GroupLeader(Game game)
            : base(game)
        {
            mGame = game;
        }

        public GroupLeader(Game game, Vector3 pPosition, Tank[] pTank, TeamColor pGroupColor)
            : base(game)
        {
            mGame = game;

            mCurrentPoint = pPosition;
            mGroupColor = pGroupColor;

            mTankArray.AddRange(pTank);

            foreach (Tank item in pTank)
            {
                InGroup tState = new InGroup();
                tState.SetGroupLeader(this);

                item.mStateMachine.CurrentState = tState;
            }
        }

        #endregion Constructor


        #region Properties

        public Vector3 CurrentPoint
        {
            get { return mCurrentPoint; }
            set { mCurrentPoint = value; }
        }

        #endregion Properties


        #region Member Functions

        void AddTank(Tank pTank)
        {
            mTankArray.Add(pTank);
        }

        #endregion Member Functions


        #region GameComponent Methods
        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            int count = 0;

            foreach (Tank item in mTankArray)
            {
                if (item.WithinGroupRadius())
                    count++;
            }

            if (count == MUSA.mObjectList.Count)
            {
                Vector3 averagePosition/*, randPoint, randTankPoint*/;
                averagePosition = new Vector3(0, 0, 0);

                //Move Group Leader to a new Position
                mCurrentPoint = new Vector3(Rand.random((mCurrentPoint.X - 15), (mCurrentPoint.X + 15)), 
                                            Rand.random((mCurrentPoint.Y - 15), (mCurrentPoint.Y + 15)), 0);
            }

            base.Update(gameTime);
        }

        #endregion
    }
}
